home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_8 / golden.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  1.3 KB  |  64 lines  |  [MATF/MATL]

  1. function [p,yp,dp,dy,A,B,C,D] = golden(f,a,b,delta,epsilon)
  2. % [p,yp,dp,dy,A,B,C,D] = golden(f,a,b,delta,epsilon)
  3. % [p,yp,dp,dy] = golden(f,a,b,delta,epsilon)
  4. % Golden ratio search for a minimum.
  5. % f  is the function, input.
  6. % a  is the left  endpoint, input.
  7. % b  is the right endpoint, input.
  8. % delta   is the tolerance for the abscissas, input.
  9. % epsilon is the tolerance for the ordinates, input.
  10. % p  is the abscissa of the minimum, output.
  11. % dp is the ordinate of the minimum, output.
  12. % dp is the error bound for  p, output.
  13. % dy is the error bound for yp, output.
  14. % A  is the vector of left  endpoints, output.
  15. % B  is the vector of right endpoints, output.
  16. % C  is the vector of central values, output.
  17. % D  is the vector of central values, output.
  18. r1 = (sqrt(5)-1)/2;
  19. r2 = r1^2;
  20. h = b - a;
  21. ya = feval(f,a);
  22. yb = feval(f,b);
  23. c = a + r2*h;
  24. d = a + r1*h;
  25. yc = feval(f,c);
  26. yd = feval(f,d);
  27. k = 1;
  28. A(k) = a;
  29. B(k) = b;
  30. C(k) = c;
  31. D(k) = d;
  32. while (abs(yb-ya)>epsilon) | (h>delta)
  33.   k = k+1;
  34.   if (yc<yd),
  35.     b = d;
  36.     yb = yd;
  37.     d = c;
  38.     yd = yc;
  39.     h = b - a;
  40.     c = a + r2*h;
  41.     yc = feval(f,c);
  42.   else
  43.     a = c;
  44.     ya = yc;
  45.     c = d;
  46.     yc = yd;
  47.     h = b - a;
  48.     d = a + r1*h;
  49.     yd = feval(f,d);
  50.   end
  51.   A(k) = a;
  52.   B(k) = b;
  53.   C(k) = c;
  54.   D(k) = d;
  55. end
  56. dp = abs(b-a);
  57. dy = abs(yb-ya);
  58. p = a;
  59. yp = ya;
  60. if (yb<ya),
  61.   p = b;
  62.   yp = yb;
  63. end
  64.